package com.md.youyouhui.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.AdapterHomeShoppingBinding;
import com.md.youyouhui.net.response.GetLimitSellResponse;
import com.md.youyouhui.ui.shopping.ShoppingPayActivity;
import com.md.youyouhui.utils.GlideApp;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/6/11.
 */
public class HomeShoppingAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterHomeShoppingBinding>> {
    List<GetLimitSellResponse> data;
    private Activity context;

    public HomeShoppingAdapter(Activity context) {
        this.context = context;
    }

    public void upDate(List<GetLimitSellResponse> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DataBoundViewHolder<AdapterHomeShoppingBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        AdapterHomeShoppingBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_home_shopping, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterHomeShoppingBinding> holder, int i) {
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) holder.binding.homeIv.getLayoutParams();
        layoutParams.width = width - 30;
        layoutParams.height = width / 2;
        holder.binding.homeIv.setLayoutParams(layoutParams);
        holder.binding.homeContent.setText(data.get(i).title);
        holder.binding.homeNum.setText(String.format("已售%s", data.get(i).sales));
        holder.binding.homePrice.setText(String.format("￥%s", data.get(i).price));
        GlideApp.with(context).load(data.get(i).cover).into(holder.binding.homeIv);
        holder.itemView.setOnClickListener(v -> context.startActivity(new Intent(context, ShoppingPayActivity.class)
                .putExtra("id", data.get(i).id)));
    }

    @Override
    public int getItemCount() {
        return null == data ? 0 : data.size();
    }
}
